package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<Department> departments = departmentService.listAll();
         model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if (id != null) {
                employeeService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        model.addAttribute("departments",departments);

        if(id!=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids) {
        try {
            if (employee.getId() != null) {
              employeeService.update(employee,ids);
            } else {
                employeeService.save(employee,ids);;
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }

    }




    @RequestMapping("/updatePassword")
    public String updatePwd(){
        return "common/updatePwd";
    }



    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword){

        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();

        try {
           employeeService.updatePwd(newPassword,oldPassword,employee.getId());
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }

    }


    @RequestMapping("/setInput")
    public String setInput(Long id,Model model){
        Employee employee = employeeService.get(id);
        model.addAttribute("employee",employee);
        return "/common/resetPwd";
    }


    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult update(String newPassword,Long id){

        try {
            employeeService.resetPwdById(newPassword, id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(@RequestParam("ids[]") Long[] ids) {
        try {
                employeeService.batchDelete(ids);
                 return new JsonResult();
        } catch (Exception e) {

            return new JsonResult(false, "删除失败");
        }
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name,Long id) {
        //由于插件返回值必须是key,value值,则可以使用map进行返回
        HashMap<String, Boolean> map = new HashMap<String, Boolean>();
        if (id != null) {
            Employee employee = employeeService.get(id);
            if (employee.getName().equals(name)) {
                map.put("valid", true);
                return map;
            }

        }
            Employee employee1 = employeeService.selectByName(name);
            map.put("valid", employee1 == null);
            return map;
    }


    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建文件
        Workbook wb = employeeService.exportXls();
        //通过流的方式写回给浏览器
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
          //导入传进来的方法
        employeeService.importxls(file);

        return new JsonResult();
    }


    @RequestMapping("/disable")
    public String disable(Long id){
         employeeService.updateDisableById(id);
         return "redirect:/employee/list.do";
    }

    @RequestMapping("/recovery")
    public String recovery(Long id){
        employeeService.updateRecoveryById(id);
        return "redirect:/employee/list.do";
    }

}